package com.chenglai.order.controller;

import com.chenglai.commonutils.common.Result;
import com.chenglai.commonutils.common.StatusCode;
import com.chenglai.order.pojo.MainOrder;
import com.chenglai.order.pojo.Order;
import com.chenglai.order.service.OrderService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.jboss.jandex.Main;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 类描述：
 *
 * @author rjh
 * @date 2023/11/10 10:29
 */
@CrossOrigin
@RestController
@RequestMapping("/order")
@Api(value = "/order", tags = {"订单服务"})
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/toAddMainOrders")
    @ApiOperation(value = "添加预付订单")
    public Result toAddMainOrders(MainOrder mainOrder) {
        try {
            Map map = orderService.toAddMainOrders(mainOrder);
            return new Result(true, StatusCode.OK, "操作成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @GetMapping("/addMainOrderWuliu")
    @ApiOperation(value = "添加订单物流信息")
    public Result addMainOrderWuliu(@ApiParam(name = "moId", value = "moId") @RequestParam("moId") Integer moId,
                                    @ApiParam(name = "orderWuliu", value = "物流单号") @RequestParam("orderWuliu") String orderWuliu,
                                    @ApiParam(name = "orderCom", value = "物流公司代码") @RequestParam("orderCom") String orderCom,
                                    @ApiParam(name = "orderWuliuName", value = "物流公司名称") @RequestParam("orderWuliuName") String orderWuliuName,
                                    @ApiParam(name = "postSum", value = "邮费") @RequestParam("postSum") Double postSum) {
        try {
            orderService.addMainOrderWuliu(moId, orderWuliu, orderCom, orderWuliuName, postSum);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @GetMapping("/getMainOrder")
    @ApiOperation(value = "获取我的订单")
    public Result getMainOrder(@ApiParam(name = "userId", value = "userId") @RequestParam("userId") Integer userId,
                               @ApiParam(name = "shopId", value = "门店ID") @RequestParam("shopId") Integer shopId,
                               @ApiParam(name = "status", value = "状态 0：全部") @RequestParam("status") Integer status) {
        try {
            List<MainOrder> mainOrders = orderService.getMainOrder(userId, shopId, status);
            return new Result(true, StatusCode.OK, "操作成功", mainOrders);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @GetMapping("/getMainOrderList")
    @ApiOperation(value = "门店获取订单")
    public Result getMainOrderList(@ApiParam(name = "page", value = "page") @RequestParam("page") Integer page,
                                   @ApiParam(name = "size", value = "size") @RequestParam("size") Integer size,
                                   @ApiParam(name = "shopId", value = "门店ID") @RequestParam("shopId") Integer shopId,
                                   @ApiParam(name = "status", value = "状态 0：全部") @RequestParam("status") Integer status) {
        try {
            PageInfo pageInfo = orderService.getMainOrderList(page, size, shopId, status);
            return new Result(true, StatusCode.OK, "操作成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @PostMapping("/updateOrderStatus")
    @ApiOperation(value = "修改订单状态")
    public Result updateOrderStatus(@ApiParam(name = "orderNum", value = "订单号") @RequestParam("orderNum") String orderNum,
                                    @ApiParam(name = "status", value = "订单状态") @RequestParam("status") Integer status,
                                    @ApiParam(name = "transactionId", value = "微信流水号") @RequestParam("transactionId") String transactionId) {
        try {
            orderService.updateOrderStatus(orderNum, status, transactionId);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @PostMapping("/updateOrderRefundStatus")
    @ApiOperation(value = "修改订单状态")
    public Result updateOrderRefundStatus(@ApiParam(name = "transactionId", value = "微信流水号") @RequestParam("transactionId") String transactionId,
                                          @ApiParam(name = "status", value = "订单状态") @RequestParam("status") Integer status) {
        try {
            orderService.updateOrderRefundStatus(transactionId, status);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @GetMapping("/queryOrderFrom")
    @ApiOperation(value = "获取订单来源 0 单独下单 1 团购")
    public Integer queryOrderFrom(@ApiParam(name = "orderNum", value = "订单号") @RequestParam("orderNum") String orderNum) {
        return orderService.queryOrderFrom(orderNum);
    }

    @GetMapping("/detailByGroupOn")
    @ApiOperation(value = "获取团购订单相关信息")
    public Map<String,Object> detailByGroupOn(@ApiParam(name = "orderNum", value = "订单号") @RequestParam("orderNum") String orderNum){
        return orderService.detailByGroupOn(orderNum);
    }

    @GetMapping("/queryRefundParam")
    @ApiOperation(value = "获取退款所需参数")
    public Map<String,Object> queryRefundParam(@ApiParam(name = "orderNum", value = "订单号") @RequestParam("orderNum") String orderNum){
        return orderService.queryRefundParam(orderNum);
    }

    @PostMapping("/updateOrderGroup")
    @ApiOperation(value = "修改订单的groupId")
    public Result updateOrderGroup(@ApiParam(name = "orderNum", value = "订单号") @RequestParam("orderNum") String orderNum,
                                   @ApiParam(name = "groupId", value = "团购主表id") @RequestParam("groupId") Integer groupId){
        orderService.updateOrderGroup(orderNum,groupId);
        return new Result(true,StatusCode.OK,"操作成功");
    }

}
